'''
Contains the NonCSNClaim class, which is used to create a new, non-CSN
warranty claim.
'''
import PySimpleGUI as sg
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from warranty_manager.claim.claim import Claim


class NonCSNClaim(Claim):
    '''
    Creates a new CSN warranty claim, or submits all Columbus City Schools
    CSN claims where no parts need to be returned, and all parts have shipped.
    '''
    def __create_new_case(self):
        '''
        Enters the serial number of the device with a problem, into the HP
        warranty website, and then clicks the button to create a new claim.
        '''
        self._wait.until(
            EC.presence_of_element_located(
                (By.XPATH, '//*[@id="SubmitnewCase"]/div/div[1]/form/input[1]')
            )
        ).send_keys(self._info['serial number'])

        self._browser.execute_script(
            "arguments[0].click();",
            self._wait.until(
                EC.element_to_be_clickable( (By.ID, 'btnCreateCase') )
            )
        )

    def __fill_in_issue_title(self):
        '''Enters the case title into the claim.'''
        self._wait.until(
            EC.presence_of_element_located( (By.ID, 'caseTitle') )
        ).send_keys(self._info['title'])

    def __fill_in_issue_description(self):
        '''Enters a description of the problem with the device.'''
        self._wait.until(
            EC.presence_of_element_located( (By.ID, 'probDesc') )
        ).send_keys(self._info['description'])

    def __fill_in_troubleshooting_steps(self):
        '''Enters the troubleshooting steps taken to try to fix the device.'''
        self._wait.until(
            EC.presence_of_element_located( (By.ID, 'troubleshootingSteps') )
        ).send_keys(
            self._info['troubleshooting'] + ' Please dropship a box, to send unit to repair center.'
        )

    def __submit_case(self):
        '''Clicks the button to submit all the provided information to HP.'''
        self._wait.until(
            EC.element_to_be_clickable( (By.ID, 'btnSubmitCase') )
        ).click()

    def get_case_id(self):
        '''Gets the case ID for the claim.'''
        return self._info['case id']

    def __check_case_submitted(self):
        '''
        Gets the claim's Case ID, so it can be put into the Cherwell ticket.
        '''
        # To make sure that the case was successfully submitted
        try:
            WebDriverWait(self._browser, timeout=30).until(
                EC.text_to_be_present_in_element(
                    (
                        By.CSS_SELECTOR,
                        'body > div.container.ng-scope > '
                        'div.container > div > div > div > h2'
                    ),
                    'Case successfully submitted'
                )
            )
            self._browser.quit()
            sg.popup('Warranty claim successfully submitted!', title='Success Message')
        except TimeoutException:
            sg.popup_error('Warranty claim was not successfully submitted!', title='Error Message')

    def create(self):
        self._log_into_hp_warranty_site()
        self._accept_cookies()
        self.__create_new_case()
        self.__fill_in_issue_title()
        self.__fill_in_issue_description()
        self.__fill_in_troubleshooting_steps()
        self.__submit_case()
        self.__check_case_submitted()
